package com.security.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.security.order.model.OrderInfo;
import com.security.order.model.PriceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author yanggld
 * @Date 2020/3/9-17:07
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderInfoController {

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping
    public OrderInfo save(@RequestBody OrderInfo orderInfo,@AuthenticationPrincipal UserDetails username,@AuthenticationPrincipal(expression = "#this.id") Long userId) {
        log.info("***************user is:{}", JSONObject.toJSON(username));
        log.info("***************user id is:{}", userId);
        PriceInfo priceInfo =
            restTemplate.getForObject("http://price-service/price/" + orderInfo.getProductId(), PriceInfo.class);
        orderInfo.setPrice(priceInfo.getPrice());
        return orderInfo;
    }
}
